package com.smartthinking.CICPNL.home.activity;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.smartthinking.CICPNL.R;


public class CuponesArrayAdapter extends BaseAdapter {
	private final Context context;
	private final List<Dictionary<String, String>> values;
	private final String server;
	Utilities util;

	public CuponesArrayAdapter(Context context, ArrayList<Dictionary<String, String>> traeDatos, String server) {
		this.context = context;
		this.values = traeDatos;
		this.server = server;
		util = new Utilities();
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.ui_cupon_items2, parent, false);


		Dictionary dic = values.get(position);

		TextView titulo = (TextView) rowView.findViewById(R.id.tv_cupon_items_promociones_disponibles);
		if(Integer.parseInt(dic.get("total").toString())>1){
			titulo.setText(""+dic.get("total").toString() + " " + context.getResources().getString(R.string.lbl_ofertas) + " " + dic.get("poi_nombre").toString());	
		}else{
			titulo.setText(""+dic.get("total").toString() + " " + context.getResources().getString(R.string.lbl_oferta) + " " + dic.get("poi_nombre").toString());
		}
		
		TextView restric = (TextView) rowView.findViewById(R.id.tv_cupon_items_mejor_oferta);
		if((dic.get("mejoroferta").toString().equals("null"))||(dic.get("mejoroferta").toString().equals(null))||
				(dic.get("mejoroferta").toString().equals(""))){
			restric.setText("");

		}else{
			restric.setText(dic.get("mejoroferta").toString());
		}

//		TextView vigencia = (TextView) rowView.findViewById(R.id.textView3);
//		vigencia.setText(dic.get("cup_vigencia").toString());
		RelativeLayout rl_foto = (RelativeLayout) rowView.findViewById(R.id.iv_cupon_items_imagen);
		
		ImageViewRounded foto = new ImageViewRounded(context);
		foto.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		ImageView foto = (ImageView) rowView.findViewById(R.id.iv_cupon_items_imagen);
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 2;
		Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+
				"/Pediatria/"+dic.get("poi_thumb").toString());
		foto.setImageBitmap(bm);
		foto.setScaleType(ScaleType.FIT_XY);
		rl_foto.addView(foto);
//		Utilities.qryImg2(context, dic.get("poi_thumb").toString(),foto);


		return rowView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return values.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	public class ImageViewRounded extends ImageView {

	    public ImageViewRounded(Context context) {
	        super(context);
	    }

	    public ImageViewRounded(Context context, AttributeSet attrs) {
	        super(context, attrs);
	    }

	    public ImageViewRounded(Context context, AttributeSet attrs, int defStyle) {
	        super(context, attrs, defStyle);
	    }

	    @Override
	    protected void onDraw(Canvas canvas) {
	        BitmapDrawable drawable = (BitmapDrawable) getDrawable();

	        if (drawable == null) {
	            return;
	        }

	        if (getWidth() == 0 || getHeight() == 0) {
	            return; 
	        }

	        Bitmap fullSizeBitmap = drawable.getBitmap();

	        int scaledWidth = getMeasuredWidth();
	        int scaledHeight = getMeasuredHeight();

	        Bitmap mScaledBitmap;
	        if (scaledWidth == fullSizeBitmap.getWidth() && scaledHeight == fullSizeBitmap.getHeight()) {
	            mScaledBitmap = fullSizeBitmap;
	        } else {
	            mScaledBitmap = Bitmap.createScaledBitmap(fullSizeBitmap, scaledWidth, scaledHeight, true /* filter */);
	        }

	        Bitmap roundBitmap = getRoundedCornerBitmap(getContext(), mScaledBitmap, 5, scaledWidth, scaledHeight,
	                false, false, false, false);
	        canvas.drawBitmap(roundBitmap, 0, 0, null);

	    }

	}


	public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR  ) {

	    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	    Canvas canvas = new Canvas(output);
	    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

	    final int color = 0xff424242;
	    final Paint paint = new Paint();
	    final Rect rect = new Rect(0, 0, w, h);
	    final RectF rectF = new RectF(rect);

	    //make sure that our rounded corner is scaled appropriately
	    final float roundPx = pixels*densityMultiplier;

	    paint.setAntiAlias(true);
	    canvas.drawARGB(0, 0, 0, 0);
	    paint.setColor(color);
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);


	    //draw rectangles over the corners we want to be square
	    if (squareTL ){
	        canvas.drawRect(0, 0, w/2, h/2, paint);
	    }
	    if (squareTR ){
	        canvas.drawRect(w/2, 0, w, h/2, paint);
	    }
	    if (squareBL ){
	        canvas.drawRect(0, h/2, w/2, h, paint);
	    }
	    if (squareBR ){
	        canvas.drawRect(w/2, h/2, w, h, paint);
	    }

	    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
	    canvas.drawBitmap(input, 0,0, paint);

	    return output;
	}
	
	
	
} 
